import { _circle, _g } from './svg.js'
import { renderShape } from './renderShape.js'
import { linkPoints, linkCrossPoints } from '../algorithm/link.js'
import { _dis, _mid } from '../math/index.js'
import { _fractal } from './fractal/fractal.js'
import { _transform } from '../math/transform.js'
import { polarPoints } from '../algorithm/polar.js'
import { _mirrorPoints } from '../algorithm/mirror.js'
import { polylinePoints } from '../algorithm/line.js'
import { _colors } from "../color/index.js"
import { _motion } from './motion.js'
import { rayMix } from "../math/arrayMix.js"
import { _show } from '../common/index.js'
import { _path2 } from './path2.js'


// 边
export const _dege = (points, options, props, children) => {
    return renderShape({
        shape: 'path', //默认path
        points,
        pathMode:'LINE_LOOP',
        closed: true,
        ...options
    }, {
        name: 'edge',
        ...props,
        ...options.props
    }, children)
}

// 中心点
export const _centre = ({ o, r }, options = {}, props = {}) => {
    return renderShape({
        shape: 'circle',
        r: r || 5,
        ...options,
        o
    }, {
        name: 'centre',
        ...props,
        ...options.props
    })
}

// 外切圆
export const _excircle = ({ o, r }, options, props) => {
    return renderShape({
        shape: 'circle',
        o,
        r
    }, {
        name: 'excircle',
        ...props,
        fill: 'none',
        ...options.props,
    })
}
// 内切圆，
export const _incircle = (o, points, opitons, props) => {
    let r = _dis(o, _mid.apply(null, points.slice(0, 2)))
    return renderShape({
        shape: 'circle',
        o, r
    }, {
        name: 'incircle',
        ...props,
        fill: 'none',
        ...opitons.props
    })
}
// 顶点
export const _vertex = (points = [], options = {}, props = {}) => {
    // 默认向心
    let { orient = true, n = points.length } = options
    return points.map((t, i) => {
        return renderShape({
            shape: 'circle', //默认圆形
            r: 5,
            o: t,
            a: orient ? 360 * i / n : 0,
            ...options,
        },
            {
                name: 'vertex',
                ...props,
                ...options.props
            })
    })
}

// 半径
export const _radius = (o = [], points = [], options = {}, props = {}) => {
    let rps = rayMix(o, points)
    return renderShape({
        shape: 'path', //默认path
        points: rps,
        closed: true,
        ...options
    }, {
        name: 'radius',
        ...props,
        ...options.props
    })
}
export const _labels = ({ points = [], render, label }, opitons = {}, props = {}) => {
    return points.map((t, index) => {
        let [x, y] = t
        return renderShape({
            shape: 'text',
            x, y,
            text: render ? render(index) : (label || index)
        }, {
            name: 'text',
            fontSize: 12,
            textAnchor: 'middle',
            dominantBaseline: 'middle',
            ...props,
            ...opitons
        })
    })
}

// 连线
export const _links = (points, options, props) => {
    let lps = linkPoints(points)

    let children = []
    if (options.crossPoints) {
        let lcps = linkCrossPoints(points)

        children = lcps.map(t => {
            return _circle({ o: t, r: 3 }, {
                fill: 'red',
                stroke: 'none'
            })
        })
    }




    children[children.length] = renderShape({
        shape: 'path', //默认path
        points: lps,
        closed: false,
        ...options
    }, {
        name: 'link',
        ...props,
        ...options
    }, children)

    return _g(children)

}

// export const _polygon=(options,props)=>{
//     let points = polarPoints(options)
//     let { transform, mirror, pathMode = 'LINE_LOOP' } = options
//     // 变形函数
//     points = _transform(points, transform)
//     points = _mirrorPoints(points, mirror)
//     return _path2({
//         ...options,
//         points,
//         pathMode,
//         mirror: null,
//         color: mirror ? mirror.color : null
//     },props)
// }
// 多边形
export const _polygon = (options, props) => {
    let {
        width = 800,
        height = 600,
        r = 100,
        n = 6,
        o = [800 / 2, 600 / 2],
        a = 0,
        centre,
        vertex,
        radius,
        excircle,
        incircle,
        links,
        labels,
        fractal,
        points,
        edge = {}, //默认有边
        transform,
        axis,
        // 路径点
        pathPoints,
        // 动画
        motion,
        color
    } = options
    //  两种方式：直接传points优先，或者 根据orn计算点， 
    points = points || polarPoints({
        o,
        r,
        n,
        a
    })

    // 变形函数
    points = _transform(points, transform)

    let container = []
    // 坐标
    if (axis) {
        container.push(renderShape({
            shape: 'axis',
            width,
            height,
            o,
            ...axis
        }))
    }
    // 路径点
    if (pathPoints) {
        // 默认向心
        let { orient = true, m } = pathPoints
        let pps = polylinePoints(points, {
            m
        })
        let colors = _colors(color, pps.length, 0.5)
        pps.map((t, i) => {
            let v = renderShape({
                shape: 'circle', //默认圆形
                r: 5,
                o: t,
                a: orient ? 360 * i / n : 0,
                ...pathPoints,
            }, {
                name: 'pathPoints',
                // stroke: 'none',
                // fill: colors[i],

                fill: 'none',
                stroke: colors[i],
                ...props,
                ...pathPoints.props
            })
            container.push(v)
        })
    }

    // 边
    if (edge) {
        let children = []
        // 动画
        if (motion) {
            if (motion.type === 'dashoffset') {
                // edge.['stroke-dasharray'] = motion['stroke-dasharray'] || '10'

                Object.assign(edge, {
                    props: {
                        'stroke-dasharray': motion['stroke-dasharray'] || '10'
                    }
                })
            }
            children.push(_motion(motion))
        }
        container.push(_dege(points, edge, props, children))
    }

    // 中心点
    if (_show(centre)) {
        container.push(_centre({ o, r: 5 }, centre, props))
    }
    // 顶点
    if (_show(vertex)) {
        container.push(..._vertex(points, vertex, props))
    }
    // 半径
    if (_show(radius)) {
        container.push(_radius(o, points, radius, props))
    }

    // 外切圆
    if (_show(excircle)) {
        container.push(_excircle({ o, r }, excircle, props))
    }
    // 内切圆
    if (_show(incircle)) {
        container.push(_incircle(o, points, incircle, props))
    }

    // 连接线
    if (_show(links)) {
        container.push(_links(points, links, props))
    }

    if (_show(labels)) {
        container.push(..._labels({ points }, labels, props))
    }


    // 分形
    if (fractal) {
        let fn = (t) => {
            let fs = _fractal({
                ...options,
                fractal: t,
                points,
                n,
                o,
            }, props)
            container.push(...fs)
        }
        if (Array.isArray(fractal)) {
            fractal.forEach(t => fn(t))
        } else {
            fn(fractal)
        }
    }

    let len = container.length
    return len ? (len === 1 ? container[0] : _g(container, {
        name: 'polygon',
        fill: 'none',
        stroke: 'black',
        ...props,
    })) : null
}

// 三角形
export const _triangle = (options, props) => {
    return _polygon({
        shape: 'polygon',
        n: 3,
        ...options,
    }, props)
}

// 正方形
export const _square = (options, props) => {
    return _polygon({
        shape: 'polygon',
        n: 4,
        ...options,
    }, props)
}
// 五边形
export const _pentagon = (options, props) => {
    return _polygon({
        shape: 'polygon',
        n: 5,
        ...options,
    }, props)
}
// 六边形
export const _hexagon = (options, props) => {
    return _polygon({
        shape: 'polygon',
        n: 6,
        ...options,
    }, props)
}
// 七边形
export const _heptagon = (options, props) => {
    return _polygon({
        shape: 'polygon',
        n: 7,
        ...options,
    }, props)
}
// 射线
export const _ray = (options = {}, props = {}) => {
    let radius = options.radius || {}
    let ray = _polygon({
        ...options,
        edge: null,
        radius
    }, props)
    return ray
}